package github.fafaovo.common.event;

import github.fafaovo.common.mycapability.ISpeedUpCapability;
import github.fafaovo.common.mycapability.SpeedUpCapabilityProvider;
import github.fafaovo.core.registry.CommonSetupEventHandler;
import github.fafaovo.deepcapacity;
import github.fafaovo.network.NetworkRegistryHandler;
import github.fafaovo.network.Packet.Power;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.network.PacketDistributor;


@Mod.EventBusSubscriber
public class ExamplePowerEvent {
    //给玩家添加能力
    @SubscribeEvent
    public static void onAttackCapabilityEvent(AttachCapabilitiesEvent<Entity> event)
    {
        Entity object = event.getObject();
        if(object instanceof PlayerEntity)
        {
            event.addCapability(new ResourceLocation(deepcapacity.MOD_ID, "speedup"), new SpeedUpCapabilityProvider());
        }
    }

    //玩家加入时将数值打印到聊天框
    @SubscribeEvent
    public static void onPlayerJoin(EntityJoinWorldEvent event)
    {
        Entity entity = event.getEntity();
        //在服务器上 实体是玩家
        if(!event.getWorld().isRemote && entity instanceof PlayerEntity)
        {
            PlayerEntity playerEntity = (PlayerEntity) entity;
            //获取到被包装过的能力
            LazyOptional<ISpeedUpCapability> capability = playerEntity.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
            capability.ifPresent((c)->{
                float Power = c.getLevel();
                //向聊天栏发送
//                playerEntity.sendMessage(
//                        new StringTextComponent("your example power is:"+Power),
//                        playerEntity.getUniqueID()
//                );
                //同步数据 往服务端发包是sendToServer
                NetworkRegistryHandler.CHANNEL.send(
                        PacketDistributor.PLAYER.with(
                                ()->(ServerPlayerEntity) playerEntity),//为了区分不同的玩家
                        new Power(c.getLevel())
                );
            });

        }
    }

    //处理死亡刷新
    @SubscribeEvent
    public static void onPlayerClone(PlayerEvent.Clone event)
    {
        //玩家死亡前的对象
        PlayerEntity original = event.getOriginal();
        //玩家死亡后的对象
        PlayerEntity player = event.getPlayer();

        LazyOptional<ISpeedUpCapability> originalcapability = original.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
        LazyOptional<ISpeedUpCapability> capability = player.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);

        if(originalcapability.isPresent() && capability.isPresent())
        {
            originalcapability.ifPresent((a)->{
                capability.ifPresent((b)->{
                    b.setLevel((int)(a.getLevel() == 0f ? 0 : a.getLevel()/60));
                    //将玩家死亡前的数值给与到死亡后
                    //向聊天栏发送
//                    player.sendMessage(
//                            new StringTextComponent("your example power is:"+a.getLevel()),
//                            player.getUniqueID()
//                    );

                    NetworkRegistryHandler.CHANNEL.send(
                            PacketDistributor.PLAYER.with(
                                    ()->(ServerPlayerEntity) player),//为了区分不同的玩家
                            new Power(b.getLevel())
                    );
                });
            });
        }


        //处理commmands 中玩家死亡数组消失的问题
        if(!original.getEntityWorld().isRemote())
        {
            player.getPersistentData().putIntArray(
                    deepcapacity.MOD_ID + ".save_pos",
                    original.getPersistentData().getIntArray(deepcapacity.MOD_ID + ".save_pos")
            );
        }
    }




}
